package com.ruoyi.web.sso.controller;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.sso.domain.DictData;
import com.ruoyi.web.sso.service.IDictDataService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 字典数据Controller
 *
 * @author ruoyi
 * @date 2021-11-04
 */
@Controller
@RequestMapping("/sso/data")
@Api(tags = "字典数据")
public class DictDataController extends BaseController
{
    private String prefix = "sso/data";

    @Autowired
    private IDictDataService dictDataService;

    @RequiresPermissions("sso:data:view")
    @GetMapping()
    @ApiOperation(value="查询列表页面",notes="查询列表页面")
    public String data()
    {
        return prefix + "/data";
    }

    /**
     * 查询字典数据列表
     */
    @RequiresPermissions("sso:data:list")
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation(value="查询列表",notes="查询列表")
    public TableDataInfo list(DictData dictData)
    {
        startPage();
        List<DictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    /**
     * 导出字典数据列表
     */
    @RequiresPermissions("sso:data:export")
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    @ApiOperation(value="导出字典数据列表",notes="导出字典数据列表")
    public AjaxResult export(DictData dictData)
    {
        List<DictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<DictData> util = new ExcelUtil<DictData>(DictData.class);
        return util.exportExcel(list, "字典数据数据");
    }

    /**
     * 新增字典数据
     */
    @GetMapping("/add")
    @ApiOperation(value="新增字典数据页面",notes="新增字典数据页面")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存字典数据
     */
    @RequiresPermissions("sso:data:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value="新增字典数据",notes="新增字典数据")
    public AjaxResult addSave(DictData dictData)
    {
        return toAjax(dictDataService.insertDictData(dictData));
    }

    /**
     * 修改字典数据
     */
    @GetMapping("/edit/{dictCode}")
    @ApiOperation(value="修改字典数据页面",notes="修改字典数据页面")
    public String edit(@PathVariable("dictCode") String dictCode, ModelMap mmap)
    {
        DictData dictData = dictDataService.selectDictDataByDictCode(dictCode);
        mmap.put("dictData", dictData);
        return prefix + "/edit";
    }

    /**
     * 修改保存字典数据
     */
    @RequiresPermissions("sso:data:edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation(value="修改字典数据",notes="修改字典数据")
    public AjaxResult editSave(DictData dictData)
    {
        return toAjax(dictDataService.updateDictData(dictData));
    }

    /**
     * 删除字典数据
     */
    @RequiresPermissions("sso:data:remove")
    @Log(title = "字典数据", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    @ApiOperation(value="删除字典数据",notes="删除字典数据")
    public AjaxResult remove(String ids)
    {
        return toAjax(dictDataService.deleteDictDataByDictCodes(ids));
    }
}
